﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Results_Comparer
{
    public class Difference
    {
        // Fields
        public static Dictionary<RdfId, List<RdfAttribute>> additions = new Dictionary<RdfId, List<RdfAttribute>>(new RdfId.EqualityComparer());
        public static Dictionary<RdfId, List<RdfAttribute>> changes = new Dictionary<RdfId, List<RdfAttribute>>(new RdfId.EqualityComparer());
        public static Dictionary<RdfId, List<RdfAttribute>> deletions = new Dictionary<RdfId, List<RdfAttribute>>(new RdfId.EqualityComparer());

        // Methods
        private static void CreateDifference(Dictionary<RdfId, List<RdfAttribute>> dict, RdfId rdfId, RdfAttribute attribute)
        {
            if (dict.ContainsKey(rdfId))
            {
                dict[rdfId].Add(attribute);
            }
            else
            {
                List<RdfAttribute> list2 = new List<RdfAttribute> {
                attribute
            };
                dict.Add(rdfId, list2);
            }
        }

        public static void CreateDifference(string type, RdfId rdfId, RdfAttribute attribute)
        {
            if (type.Equals("A"))
            {
                CreateDifference(additions, rdfId, attribute);
            }
            else if (type.Equals("D"))
            {
                CreateDifference(deletions, rdfId, attribute);
            }
            else if (type.Equals("C"))
            {
                CreateDifference(changes, rdfId, attribute);
            }
        }

        public static Dictionary<RdfId, List<RdfAttribute>> GetAllDifferences()
        {
            Dictionary<RdfId, List<RdfAttribute>> first = new Dictionary<RdfId, List<RdfAttribute>>(new RdfId.EqualityComparer());
            return Merge(Merge(Merge(first, additions), changes), deletions);
        }

        public static int GetCounts(Dictionary<RdfId, List<RdfAttribute>> dict, string rdfType)
        {
            int num = 0;
            foreach (KeyValuePair<RdfId, List<RdfAttribute>> pair in dict)
            {
                if (pair.Key.rdfType.Equals(rdfType))
                {
                    num++;
                }
            }
            return num;
        }

        private static Dictionary<RdfId, List<RdfAttribute>> Merge(Dictionary<RdfId, List<RdfAttribute>> first, Dictionary<RdfId, List<RdfAttribute>> second)
        {
            if (second != null)
            {
                foreach (KeyValuePair<RdfId, List<RdfAttribute>> pair in second)
                {
                    if (!first.ContainsKey(pair.Key))
                    {
                        first.Add(pair.Key, pair.Value);
                    }
                }
            }
            return first;
        }
    }
}